<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>更新APK</title>
    <meta name="ctx" th:content="${#httpServletRequest.getContextPath()}"/>
    <link rel="stylesheet" th:href="@{/css/admin/base-v2.css}">
    <link rel="stylesheet" th:href="@{/lib/font-awesome/4.7.0/css/font-awesome.min.css}">
    <link rel="stylesheet" th:href="@{/css/admin/app.css}">
    
    <style>
        .file-desc {
            width: 100%;
            border: 1px solid #ededed;
            padding: 10px;
        }

        .block {
            margin-bottom: 20px;
        }

    </style>	
</head>

<body>
<div class="app-page">
    <div class="app-page-content">
        <div class="app-card">
            <div class="app-card-body">
                <div class="app-card-body-con">
                    <div class="page-container">
                        <form class="form">
							<input type="hidden" name="md5" data-name="md5" th:value="${deviceUpgrade.md5}">
							<input type="hidden" name="url" data-name="url" th:value="${deviceUpgrade.url}">
							<input type="hidden" name="size" data-name="size" th:value="${deviceUpgrade.size}">
							<input type="hidden" name="deviceUpgradeId" th:value="${deviceUpgrade.deviceUpgradeId}" data-name="deviceUpgradeId">
							<div class="form-item row">
		                        <div class="label">
		                            <label>上传APK</label>
		                        </div>
		                        <div class="body" style="min-width: 350px;">
		                            <div class="upload upload-file z-mab" data-limit="1"
		                                 th:attr="data-url=@{/software/upload/apk}" data-fieldname="files"
		                                 data-upload-success="uploadSuccess">
		                                <div class="upload-list upload-list-text"></div>
		                                <div class="upload-tips">只能上APK文件，格式 .apk</div>
		                                <div class="upload-error"></div>
		                                <div class="input-opacity">
		                                    <input type="file" class="input" accept=".apk">
		                                    <a class="z-btn z-default"><i class="fa fa-upload"></i> 上传</a>
		                                </div>
		                            </div>
		                        </div>
		                    </div>
                            <div class="form-item row">
                                <div class="label">
                                    <label>apk名称：</label>
                                </div>
                                <div class="body"> <input type="text" name="upgradeName" data-name="upgradeName" th:value="${deviceUpgrade.upgradeName}" disabled="disabled"></div>
                            </div>
                            <div class="form-item row">
                                <div class="label">
                                    <label>版本名称：</label>
                                </div>
                                <div class="body"> <input type="text" name="versionName" data-name="versionName" th:value="${deviceUpgrade.versionName}" disabled="disabled"> </div>
                            </div>
                            <div class="form-item row">
                                <div class="label">
                                    <label>版本号：</label>
                                </div>
                                <div class="body"> <input type="text" name="versionCode" data-name="versionCode" th:value="${deviceUpgrade.versionCode}" disabled="disabled"> </div>
                            </div>
                            <div class="form-item row">
                                <div class="label">
                                    <label>机型：</label>
                                </div>
                                <div class="body">
                                	<div th:each="deviceModel: ${deviceModelList}" style="display:inline-block">
                                		 <input class="input" type="checkbox" name="modelName" th:value="${deviceModel.deviceModel}"/>
                        				 <label for="" th:text="${deviceModel.deviceModel}"></label>
                                	</div>
                                </div>
                            </div>
                            <div class="form-item row">
                                <div class="label"><label>版本说明：</label></div>
                                <div class="body">
                                    <textarea class="z-df file-desc" name="description" 
                                    th:text="${deviceUpgrade.description}"
                                    data-name="description" cols="30" rows="10" placeholder="请填写说明信息（选填）"></textarea>

                                </div>
                            </div>
                            <div class="form-item row">
                                <div class="body">
                                    <a class="btn-sub z-btn z-assist btn-submit">保存</a>
                                     <a class="z-btn z-default" onclick="back();">返回</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/lib/jquery.min.js}"></script>
<script th:src="@{/lib/layer-v3.1.1/layer/layer.js}"></script>
<script th:src="@{/js/popup.js}"></script>
<script th:src="@{/js/form.js}"></script>
<script th:src="@{/js/admin/app.js}"></script>
<script th:src="@{/js/admin-upload.js}"></script>
<script>
	//获取context path
	var context = $("meta[name='ctx']").attr("content");
	var checkedModel = new Array();
	checkedModel = '[[${deviceUpgrade.model}]]';
	$(function () {
		
		//初始化CheckBox
		$('[name=modelName]').each(function(idx){
			
			if (checkedModel.indexOf(this.value) > -1) {
				this.checked = true;
			}
		})
		
        // 保存
        $("form").form({
            submitSelector: ".btn-submit",
            msg: function (msg) {
                popup.tipsTop(msg, 'warning');
            },
            submit: function () {

            	   var data = {};
                   $.each($(".form").serializeArray(), function (index, field) {
                       if (data[field.name]) {
                           data[field.name] += "," + field.value;
                       } else {
                           data[field.name] = field.value;
                       }
                   });
                   data['upgradeName'] = $(".form [name=upgradeName]").val();
                   data['versionName'] = $(".form [name=versionName]").val();
                   data['versionCode'] = $(".form [name=versionCode]").val();
                   //apk文件是否上传
                   if (!data.url) {
                       return popup.tipsTop('请上传APK', 'warning');
                   }
                   //是否选择机型
                   if (!data.modelName) {
                       return popup.tipsTop('请选择机型', 'warning');
                   }
                   //保存APK
                   $.ajax({
                       type: "POST",
                       url: context + "/software/upgradeApk",
                       data: data,
                       success: function (obj) {
                           var code = obj.code;
                           if (code == 0) {
                               popup.tipsTop('修改成功', 'success');
                               setTimeout(function (args) {
                                   //跳转到列表界面
                            	   parentOpen(context + "/software/list");
                               }, 2000);
                           } else {
                               popup.tipsTop(obj.msg, 'warning');
                           }
                       }
                   })
            }
        })
    });
    function parentOpen(url) {
  		$(window.parent.window)[0].location.href = url;
    }
	
	function back() {
		 parentOpen(context + "/software/list");
	}
	
	// 上传成功回调
    function uploadSuccess(data) {
    	var fileData = data[0];
        // 赋值apk名称和版本号
        $('[name="upgradeName"]').val(fileData["name"]);
        $('[name="versionName"]').val(fileData["versionName"]);
        $('[name="versionCode"]').val(fileData["versionCode"]);
        $('[name="size"]').val(fileData["size"]);
        $('[name="url"]').val(fileData["fileURL"]);
        $('[name="md5"]').val(fileData["md5"]);
    }
	
</script>
<script>
</script>

</body>

</html>